<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Tree_model extends CI_Model {
	
	public function getNode($uid) {

        $query	= $this->db->get_where('tree', array('uid' => $uid));
        $result	= $query->result_array();
        
        if ($result)
            return $result[0];
        else
            return false;
		
	}
	
	public function addNode($node = array()) {
		
		$node['date_created']	= date('Y-m-d H:i:s');
		
		return $this->db->insert('tree', $node);
		
	}
	
	public function addBranch($node = array()) {
		
		$this->db->trans_start();
		
		$this->db->query('UPDATE tree SET rgt = rgt + 2 WHERE rgt > ? AND root = ?', 
							array($node['pNode']['lft'], $node['root']));
		
		$this->db->query('UPDATE tree SET lft = lft + 2 WHERE lft > ? AND root = ?', 
							array($node['pNode']['lft'], $node['root']));
		
		unset($node['pNode']);					
							
		$this->addNode($node);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return FALSE;
		else
			return TRUE;
	}
	
	public function getChild($node = array()) {
		
		$query	= $this->db->query(
								'SELECT * FROM tree 
									WHERE root = ? AND lft > ? AND lft < ?
									ORDER BY lvl ASC LIMIT ?, ?',
								array(
									$node['root'],
									$node['lft'],
									$node['rgt'],
									$node['offset'],
									$node['limit']
								)
					);
        $result	= $query->result_array();
        
        if ($result)
            return $result;
        else
            return false;
		
	}
	
	public function countChild($node = array()) {
		
		$query	= $this->db->query(
								'SELECT * FROM tree 
									WHERE root = ? AND lft > ? AND lft < ?',
								array(
									$node['root'],
									$node['lft'],
									$node['rgt']
								)
					);
					
        return $query->num_rows();
	}
	
	public function selectAncestor($node = array()) {
		
		$query = $this->db->query(
								'SELECT * FROM tree 
									WHERE root = ? AND lft < ? AND rgt > ?',
								array(
									$node['root'],
									$node['lft'],
									$node['rgt']
								)
					);
					
		$result	= $query->result_array();
        
        if ($result)
            return $result;
        else
            return false;
		
	}
	
	public function addCashout($node = array()) {
		
		return $this->db->insert('cashout', $node);
		
	}
	
	public function countComission($node = array()) {
		
		$query	= $this->db->query(
								'SELECT SUM(price) AS commission FROM cashout 
									WHERE recipient = ? AND status = 0',
								array(
									$node['uid']
								)
					);
					
		$result	= $query->result_array();
        
        if ($result)
            return $result[0];
        else
            return false;
		
	}
	
	public function cashoutRequest($node = array()) {
		
		$query	= $this->db->query(
								'UPDATE cashout
									SET status = 1, date_cashout_request = NOW() 
									WHERE recipient = ? AND status = 0',
								array(
									$node['uid']
								)
					);
					
		return $query;
		
	}
	
	public function cashoutHistory($node = array()) {
		
		$query	= $this->db->query(
								'SELECT SUM(price) AS commission,
										date_cashout_request,
										date_cashout_approved,
										status  
									FROM cashout 
									WHERE recipient = ? AND status != 0
									GROUP BY date_cashout_request
									LIMIT ?,?',
								array(
									$node['uid'],
									$node['offset'],
									$node['limit']
								)
					);
					
		$result	= $query->result_array();
		
		if ($result)
            return $result;
        else
            return false;
	}
	
	public function countCashoutHistory($node = array()) {
		
		$query	= $this->db->query(
								'SELECT SUM(price) AS commission,
										date_cashout_request,
										date_cashout_approved,
										status  
									FROM cashout 
									WHERE recipient = ? AND status != 0',
								array(
									$node['uid']
								)
					);
					
		$result	= $query->num_rows();
		
		if ($result)
            return $result;
        else
            return false;
	}
	
	public function getCashoutRequest($offset, $limit) {
		
		$query = $this->db->query('SELECT SUM(price) AS commission,
											recipient, 
											date_cashout_request,
											date_cashout_approved,
											status  
									FROM cashout 
									WHERE status != 0
									GROUP BY recipient, date_cashout_request
									LIMIT ?,?',
									array(
										$offset, $limit
									)
				);
				
		$result	= $query->result_array();
		
		if ($result)
            return $result;
        else
            return false;
		
	}
	
	public function countCashoutRequest() {
		
		$query = $this->db->query('SELECT SUM(price) AS commission,
											recipient, 
											date_cashout_request,
											date_cashout_approved,
											status  
									FROM cashout 
									WHERE status != 0
									GROUP BY recipient, date_cashout_request'
				);
				
		$result	= $query->num_rows();
		
		if ($result)
            return $result;
        else
            return false;
		
	}
	
	public function approveCashout($uid, $date) {
		
		$query = $this->db->query('UPDATE cashout
									SET status = 2, date_cashout_approved = NOW() 
									WHERE 
										status = 1 AND 
										recipient = ? AND 
										date_cashout_request = ?',
									array($uid, $date)
				);
				
		return $query;
		
	}
}